package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverRankPlayActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverRecomentAlbumActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.DiscoverRankSecondAdapter;
import com.qiaoyanqing.ximalaya.discovermodule.discover_callback.DiscoverRankSecondService;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondDataEntity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.DiscoverRankSecondEntity;
import com.qiaoyanqing.ximalaya.url.Urlconfig;
import com.qiaoyanqing.ximalaya.util.MyLog;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverRankSecondFragment extends Fragment implements AbsListView.OnScrollListener,Callback<DiscoverRankSecondEntity>, SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemClickListener {
    private DiscoverRankSecondAdapter adapter;
    private ListView listView;
    private List<DiscoverRankSecondDataEntity> list;
    private String key;
    private String contentType;
    private int pageId=1;
    private int pageSize=20;
    private boolean isBottom=false;
    private SwipeRefreshLayout swipeRefreshLayout;
    private boolean isLoadMore;




    public DiscoverRankSecondFragment() {
        // Required empty public constructor
    }

    public static DiscoverRankSecondFragment newInstance(String text,String key){
        Bundle args=new Bundle();
        args.putString("text",text);
        args.putString("key",key);
        DiscoverRankSecondFragment fragment=new DiscoverRankSecondFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_rank_second,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        swipeRefreshLayout= ((SwipeRefreshLayout) view.findViewById(R.id.rank_swipe_refresh_layout));
        swipeRefreshLayout.setOnRefreshListener(this);
        listView= ((ListView) view.findViewById(R.id.rank_second_list_view));
        TextView textView= ((TextView) view.findViewById(R.id.empty_view));
        listView.setEmptyView(textView);
        list=new ArrayList<>();
        loadDatas(pageSize);
        adapter=new DiscoverRankSecondAdapter(getContext(),list,contentType);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(this);
        listView.setOnItemClickListener(this);
    }
    public void loadDatas(int pageSize){
        contentType=getActivity().getIntent().getStringExtra("contentType");
        Bundle bundle=getArguments();
        key=bundle.getString("key");
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Urlconfig.RANK_SECOND_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
            DiscoverRankSecondService service=retrofit.create(DiscoverRankSecondService.class);

            service.getDatas(contentType,"android",key,1,pageSize,2).enqueue(this);

    }


    @Override
    public void onResponse(Call<DiscoverRankSecondEntity> call, Response<DiscoverRankSecondEntity> response) {

        String title=response.body().getTitle();
        list.removeAll(list);
        list.addAll(response.body().getList());
        adapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
        isLoadMore=true;
    }
    @Override
    public void onFailure(Call<DiscoverRankSecondEntity> call, Throwable t) {
        Log.d("retrofit","网络异常");
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        if(isBottom&&i==SCROLL_STATE_IDLE&&pageSize<100&&isLoadMore){
            isLoadMore=false;
            pageSize+=20;
            loadDatas(pageSize);
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        boolean isSwipeEnabled=false;
        if(listView.getChildCount()>0){
            boolean isFirst=listView.getFirstVisiblePosition()==0;
            boolean isTop=listView.getChildAt(0).getTop()==0;

            isSwipeEnabled=isFirst&&isTop;
        }
        swipeRefreshLayout.setEnabled(isSwipeEnabled);
        if(i+i1==i2){
            isBottom=true;
        }else {
            isBottom=false;
        }
    }

    @Override
    public void onRefresh() {
        list.clear();
        pageSize=20;
        loadDatas(pageSize);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (contentType){
            case "track":
                play(i);
                break;
            case "album":



                albumDetail(i);


                break;
        }
    }

    private void albumDetail(int i) {
        Intent intent=new Intent(getContext(), DiscoverRecomentAlbumActivity.class);

//        String  icpaths=intent.getStringExtra("iconpath");
//        String  nicknames=intent.getStringExtra("nickname");
//        String  title=intent.getStringExtra("title");
//        int playcounts=intent.getIntExtra("playcounts",8414060);
        intent.putExtra("iconpath",list.get(i).getCoverSmall());
        intent.putExtra("nickname",list.get(i).getNickname());
        intent.putExtra("title",list.get(i).getTitle());
        intent.putExtra("playcounts",list.get(i).getPlaysCounts());
        intent.putExtra("album",list.get(i).getAlbumId());
        MyLog.i("SSSSSSSSSSSAAAAAAAAAA",list.get(i).getAlbumId()+"");
        startActivity(intent);

    }

    private void play(int position) {
        Intent intent=new Intent(getContext(),DiscoverRankPlayActivity.class);
        ArrayList<String>title=new ArrayList<>();
        ArrayList<String>url=new ArrayList<>();
        for (int i = 0; i <list.size() ; i++) {
            title.add(list.get(i).getTitle());
            MyLog.i("AAAA",list.get(i).getTitle());
            url.add(list.get(i).getPlayPath32());
        }
        intent.putStringArrayListExtra("title",title);
        intent.putStringArrayListExtra("url",url);
        intent.putExtra("position",position);
        startActivity(intent);
    }
}
